
# 参考 cmakes/FindMyLibShared.cmake

set(LIB_COMPONENTS libA libB)
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
    set(LIB_SUFFIX "d")
else ()
    set(LIB_SUFFIX "")
endif ()

get_filename_component(INSTALL_PATH "${CMAKE_CURRENT_LIST_DIR}/../../../" REALPATH)
message(STATUS "MyModule INSTALL_PATH:${INSTALL_PATH}")

set(MyModuleIncludes)
set(MyModuleLibs)

foreach(component ${LIB_COMPONENTS})
    set(includeFileName "${INSTALL_PATH}/include/${component}.hpp")
    set(libFileName "${INSTALL_PATH}/lib/${component}${LIB_SUFFIX}.lib")

    if(NOT EXISTS "${includeFileName}")
        message(WARNING "MyModule: Include file doesn't exist: '${includeFileName}'. MyModule installation may be broken. Skip...")
    else()
        list(APPEND MyModuleIncludes ${includeFileName})
    endif()

    if(NOT EXISTS "${libFileName}")
        message(WARNING "MyModule: library file doesn't exist: '${libFileName}'. MyModule installation may be broken. Skip...")
    else()
        list(APPEND MyModuleLibs ${libFileName})
    endif()

    unset(includeFileName)
    unset(libFileName)
endforeach()


# ======================================================
# 库文件搜索
# ======================================================
# 包含导出配置的 *.cmake 文件
include(${CMAKE_CURRENT_LIST_DIR}/MyModule.cmake)

add_library(MyModule INTERFACE)
foreach(component ${LIB_COMPONENTS})
    target_link_libraries(MyModule INTERFACE My::${component}) # My::是install(EXPORT...)设置的NAMESPACE

    # 如果库没有提供对应的版本，使用其他版本的库，例如RELEASE版本使用RelWithDebInfo版本。 Debug Release RelWithDebInfo MinSizeRel
#    set_target_properties(My::${component} PROPERTIES
#            MAP_IMPORTED_CONFIG_DEBUG Release
#            MAP_IMPORTED_CONFIG_RELEASE RelWithDebInfo
#            MAP_IMPORTED_CONFIG_MINSIZEREL Release
#            MAP_IMPORTED_CONFIG_RELWITHDEBINFO Release
#    )
endforeach()

# ======================================================
# 搜寻的结果、状态
# ======================================================
include(FindPackageHandleStandardArgs)
# 如果 MyModuleLibs 或 MyModuleIncludes 为空，则 MyModule_FOUND 将被设置为 FALSE，并在使用 find_package(MyModule REQUIRED) 时也会报错
# 否则 MyModule_FOUND 设置为 TRUE
find_package_handle_standard_args(
        MyModule
        REQUIRED_VARS MyModuleLibs MyModuleIncludes
)